package com.card.aac.controller;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.beans.propertyeditors.StringTrimmerEditor;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.card.aac.domain.Aac;
import com.card.aac.service.AacService;
import com.card.framework.page.model.PageModel;
import com.card.framework.util.CommonUtil;

@Controller
@RequestMapping("/aac")
public class AacController {
	@Autowired(required=true) 
	private AacService aacServices;
	@InitBinder
	public void setupDataBinder(WebDataBinder dataBinder) {
		//表单页面如果有时间类型的需要initbinder
		dataBinder.registerCustomEditor(String.class,new StringTrimmerEditor(false));  
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		dateFormat.setLenient(false);
		dataBinder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
	}
	/**
	 * 跳转到管理首页
	 */
	@RequestMapping("/index")
	public String aac(HttpServletRequest request,HttpSession session,
			HttpServletResponse response) throws IOException {
		return "/aac/aacList";
	}
	/**
	 * 跳转到选择商户页面.
	 */
	@RequestMapping("/aacAddList")
	public String aabList(HttpServletRequest request,HttpSession session,
			HttpServletResponse response) throws IOException {
		return "/aac/aabList";
	}
	/**
	 * ajax异步查询aab列表,返回jsondata
	 */
	@RequestMapping(value="/queryList",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> queryList(Aac aac,PageModel pageModel) throws Exception{
		return aacServices.selectAacForPage(aac, pageModel);
	}
	/**
	 * 跳转到编辑页面<br>
	 * 根据id判断是新增还是修改
	 */
	@RequestMapping("/aacAddWindow")
	public String aacAddWindow(HttpServletRequest request,HttpSession session,
			HttpServletResponse response) throws IOException {
		String id = request.getParameter("id");
		Aac aac = null;
		if(StringUtils.isNotEmpty(id)){
			aac = aacServices.getById(Long.parseLong(id));
		}else{
			aac = new Aac();
			aac.setCreatdate(new Date());
			SecurityContext sc = (SecurityContext)request.getSession().getAttribute("SPRING_SECURITY_CONTEXT");
			aac.setCreatuserid(sc.getAuthentication().getName());
		}
		request.setAttribute("aac", aac);
		return "/aac/aacPopWindow";
	}
	@RequestMapping("/aacUpdateWindow")
	public String aacUpdateWindow(HttpServletRequest request,HttpSession session,
			HttpServletResponse response) throws IOException {
		String id = request.getParameter("id");
		Aac aac = null;
		if(StringUtils.isNotEmpty(id)){
			aac = aacServices.getById(Long.parseLong(id));
		}else{
			aac = new Aac();
			aac.setCreatdate(new Date());
			SecurityContext sc = (SecurityContext)request.getSession().getAttribute("SPRING_SECURITY_CONTEXT");
			aac.setCreatuserid(sc.getAuthentication().getName());
		}
		request.setAttribute("aac", aac);
		return "/aac/aacPopWindow";
	}
//	/**
//	 * 提交新增或修改,根据id是否为空判断.
//	 * 返回提交成功后的数据.json格式
//	 */
//	@RequestMapping(value="/addOrUpdate",method=RequestMethod.POST)
//	@ResponseBody
//	public Map<String, String> addOrUpdate(Aac aac) throws Exception{
//		Map<String, String> map = new HashMap<String, String>();
//		try {
//			aacServices.addOrUpdate(aac);
//			map.put("mes", "操作成功");
//		}
//		catch (Throwable e) {
//			CommonUtil.throwsException(map, e);
//		}
//		return map; 
//	}
	
	/**
	 * 提交新增或修改,根据id是否为空判断.
	 * 返回提交成功后的数据.json格式
	 */
	@RequestMapping(value="/aacAdd",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, String> aacAdd(Aac aac) throws Exception{
		Map<String, String> map = new HashMap<String, String>();
		try {
			aacServices.addOrUpdate(aac);
			map.put("mes", "操作成功");
		}
		catch (Throwable e) {
			CommonUtil.throwsException(map, e);
		}
		return map; 
	}
		
	/**
	 * 提交新增或修改,根据id是否为空判断.
	 * 返回提交成功后的数据.json格式
	 */
	@RequestMapping(value="/aacUpdate",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, String> aacUpdate(Aac aac) throws Exception{
		System.out.println(aac.getIpaddress());
		Map<String, String> map = new HashMap<String, String>();
		try {
			aacServices.addOrUpdate(aac);
			map.put("mes", "操作成功");
		}
		catch (Throwable e) {
			CommonUtil.throwsException(map, e);
		}
		return map; 
	}
	
	/**
	 * 删除
	 */
	@RequestMapping(value="/delete",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, String> delete(@RequestParam("json") String json){
		Map<String, String> map = new HashMap<String, String>();
		try {
			aacServices.deleteAacs(CommonUtil.getBean(json, Aac.class));
			map.put("mes", "删除成功");
		} catch (Throwable e) {
			CommonUtil.throwsException(map, e);
		}
		return map;//重定向
	}
}
